package com.yjt.fresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.api.ApiCheckPassword;
import com.yjt.fresh.api.ApiLogin;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanResult;
import com.yjt.fresh.bean.BeanUserInfo;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.PreferenceManager;
import com.yjt.fresh.widget.CustomToast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by UPC on 2018/8/20.
 */

public class AtyChangePassword extends BaseSimpleActivity {
    @BindView(R.id.rlBack)
    RelativeLayout mRlBack;
    @BindView(R.id.edtAtyChangePassword)
    EditText mEdtPassword;
    @BindView(R.id.btnAtyChangePasswordNext)
    Button mBtnNext;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_change_password;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

    }

    @Override
    protected void initEvent() {
        mEdtPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (count > 0){
                    mBtnNext.setClickable(true);
                    mBtnNext.setBackgroundResource(R.drawable.selector_43b624_00b24f_5);
                }else {
                    mBtnNext.setClickable(false);
                    mBtnNext.setBackgroundResource(R.drawable.shape_e6e6e6_5);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkPassword();

            }
        });

        mBtnNext.setClickable(false);

        mRlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void checkPassword() {
        ApiCheckPassword apiCheckPassword = new ApiCheckPassword(new HttpOnNextListener<BeanResult>() {
            @Override
            public void onNext(BeanResult beanResult) {
                if ("1".equals(beanResult.getIscollect())) {
                    Intent intent = new Intent(mContext,AtyNewPassword.class);
                    intent.putExtra(MyConstants.PASSWORD,mEdtPassword.getText().toString());
                    startActivity(intent);
                    finish();
                }else {
                    CustomToast.show("密码不正确");
                }
            }

            @Override
            public void failed(String massage) {
                super.failed(massage);
                CustomToast.show(massage);
            }

            @Override
            public void onReLogin() {

            }
        },mContext,true,false,false);
        Map<String,Object> map = new HashMap<>();
        map.put("OLD_LOGIN_PASSWORD",mEdtPassword.getText().toString());
        map.put("SESSION_ID",PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        apiCheckPassword.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiCheckPassword);
    }

    HttpOnNextListener<BeanUserInfo> mLoginListener = new HttpOnNextListener<BeanUserInfo>() {
        @Override
        public void onNext(BeanUserInfo beanUserInfo) {

        }

        @Override
        public void failed(String massage) {
            CustomToast.showLong("密码错误");
        }

        @Override
        public void onReLogin() {

        }
    };
    private void toMyLogin() {
        ApiLogin apiLogin = new ApiLogin(mLoginListener,mContext,true,false,true);
        Map<String, Object> map = new HashMap<>();
        map.put("PHONE", PreferenceManager.getInstance().getUserInfo().getPHONE());
        map.put("LOGIN_PASSWORD", mEdtPassword.getText().toString());
        apiLogin.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiLogin);
    }
}
